/**
 * GeoSOA/olap4js/AxisMeta.js
 *
 * AxisMeta object
 *
 * Copyright (C) 2009 GeoSOA Research Group, Universite Laval
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Etienne Dube
 * @since 20090421
 **/

dojo.provide("GeoSOA.olap4js.AxisMeta");

dojo.require("GeoSOA.olap4js.CellSet");

/**
 * AxisMeta object
 * 
 * @param {Object} jsonAxisMeta
 * @param {Number} ordinal The ordinal of this AxisMeta (-1 if filter axis)
 * @param {Object} cellSetMeta CellSetMeta object this AxisMeta belongs to
 *
 * @constructor
 */
GeoSOA.olap4js.AxisMeta = function (jsonAxisMeta, ordinal, cellSetMeta) {
	
	this.cellSetMeta = cellSetMeta;
	
	// -1 axis ordinal is for the filter (slicer) axis
	this.ordinal = ordinal;
	
	this.hierarchies = [];
	for (var i = 0; i < jsonAxisMeta.hierarchies.length; i++) {
		this.hierarchies[i] = new GeoSOA.olap4js.Hierarchy(jsonAxisMeta.hierarchies[i], this);
	}
	
};

// AxisMeta methods:

/**
 * Returns a Hierarchy in this AxisMeta, identified by its name
 * 
 * @return The Hierarchy object
 * @type Object
 * @public
 */
GeoSOA.olap4js.AxisMeta.prototype.getHierarchyByName = function (hierarchyName) {

	for (var i = 0; i < this.hierarchies.length; i++) {
		if (this.hierarchies[i].name == hierarchyName) {
			return this.hierarchies[i];
		}
	}
	
	return null;
	
};

/**
 * Returns the count of hierarchies in this axis
 * 
 * @return Count of hierarchies
 * @type Number
 * @public
 */
GeoSOA.olap4js.AxisMeta.prototype.getHierarchyCount = function () {
	return this.hierarchies.length;
};

/**
 * Returns the CellSetAxis object corresponding to this AxisMeta
 * 
 * @return The CellSetAxis object
 * @type Object
 * @public
 */
GeoSOA.olap4js.AxisMeta.prototype.getCellSetAxis = function () {
	return this.cellSetMeta.cellSet.getCellSetAxis(this.ordinal);
};



/**
 * Hierarchy object
 * 
 * @param {Object} jsonHierarchy
 *
 * @constructor
 */
GeoSOA.olap4js.Hierarchy = function (jsonHierarchy) {
	
	this.name = jsonHierarchy.name;
	this.dimensionName = jsonHierarchy.dimensionName; // create a Dimension object instead?
	this.isParentChild = jsonHierarchy.isParentChild || false;
	
	this.levels = [];
	for (var i = 0; i < jsonHierarchy.levels.length; i++) {
		this.levels[i] = new GeoSOA.olap4js.Level(jsonHierarchy.levels[i], this);
	}
	
};

// Hierarchy methods:

/**
 * Returns a Level in this Hierarchy, identified by its name 
 * 
 * @param {String} levelName The name of the level
 * 
 * @return The Level object
 * @type Object
 * @public
 */
GeoSOA.olap4js.Hierarchy.prototype.getLevelByName = function (levelName) {

	for (var i = 0; i < this.levels.length; i++) {
		if (this.levels[i].name == levelName) {
			return this.levels[i];
		}
	}
	
	return null;

};



/**
 * Level object
 * 
 * @param {Object} jsonLevel
 * @param {Object} hierarchy The Hierarchy object this Level belongs to
 *
 * @constructor
 */
GeoSOA.olap4js.Level = function (jsonLevel, hierarchy) {
	
	this.hierarchy = hierarchy;
	this.name = jsonLevel.name;
	
	this.properties = [];
	for (var i = 0; i < jsonLevel.properties.length; i++) {
		this.properties[i] = new GeoSOA.olap4js.Property(jsonLevel.properties[i]);
	}
	
};

// Level methods:

/**
 * Returns a Property in this Level, identified by its name 
 * 
 * @param {String} propName The name of the property
 * 
 * @return The Property object
 * @type Object
 * @public
 */
GeoSOA.olap4js.Level.prototype.getPropertyByName = function (propName) {

	for (var i = 0; i < this.properties.length; i++) {
		if (this.properties[i].name == propName) {
			return this.properties[i];
		}
	}
	
	return null;
	
};



/**
 * Property object
 * 
 * @param {Object} jsonProperty
 *
 * @constructor
 */
GeoSOA.olap4js.Property = function (jsonProperty) {
	
	this.name = jsonProperty.name;
	
	// type is constrained to the following
	// TODO: use JSONSchema types?
	switch(jsonProperty.type.toLowerCase()) {
	case "boolean":
	case "integer":
	case "number":
	case "string":
	case "geometry":
	case "any":
		this.type = jsonProperty.type.toLowerCase();
		break;
	default:
		this.type = "any";
	}
	
};
